package com.sky.module.system.Department.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.module.system.Department.dao.DepartmentMapper;
import com.sky.module.system.Department.domain.entity.Department;
import com.sky.module.system.Department.domain.vo.DepartmentQueryVO;
import com.sky.module.system.Department.service.DepartmentService;
import com.sky.module.system.user.dao.UserMapper;
import com.sky.module.system.user.domain.entity.User;
import com.sky.module.system.utils.DepartmentTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zsg
 * @since 2023-02-20
 */
@Transactional
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Department> findDepartmentList(DepartmentQueryVO departmentQueryVO) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(departmentQueryVO.getDepartmentName())
                , Department::getDepartmentName, departmentQueryVO.getDepartmentName());
        queryWrapper.orderByDesc(Department::getOrderNum);
        List<Department> departments = baseMapper.selectList(queryWrapper);
        List<Department> departmentTree = DepartmentTree.makeDepartmentTree(departments, 0L);
        return departmentTree;
    }

    @Override
    public List<Department> findParentDepartment() {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Department::getOrderNum);
        final List<Department> departmentList = baseMapper.selectList(queryWrapper);
//        创建部门对象
        Department department = new Department();
        department.setId(0L);
        department.setDepartmentName("顶级部门");
        department.setPid(-1L);
        departmentList.add(department);
        List<Department> makeDepartmentTree = DepartmentTree.makeDepartmentTree(departmentList, -1L);
        return makeDepartmentTree;
    }

    /***
     * @description 判断部门下是否有子部门
     *
     * @return boolean
     * @date 2023/2/22 10:29
     *
     */
    @Override
    public boolean hasChildrenOfDepartment(Long id) {
        final LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getPid, id);
        if (baseMapper.selectCount(queryWrapper) > 0) {
            return true;
        }
        return false;
    }

    /***
     * @description 判断部门下是否有用户
     * @param id
     * @return boolean
     * @date 2023/2/22 10:43
     *
     */
    @Override
    public boolean hasUserOfDepartment(Long id) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getDepartmentId, id);
//        如果数量大于0表示存在
        if (userMapper.selectCount(queryWrapper) > 0) {
            return true;
        }
        return false;
    }
}
